<!DOCTYPE html>
<html>
    <head>
        <script src="/w3c/resources/testharness.js"></script>
        <script src="/w3c/resources/testharnessreport.js"></script>
        <script src="mediasource-util.js"></script>
        <link rel='stylesheet' href='/w3c/resources/testharness.css'>
    </head>
    <body>
        <div id="log"></div>
        <script>
          mediasource_testafterdataloaded(function(test, mediaElement, mediaSource, segmentInfo, sourceBuffer, mediaData)
          {
              var previousQuality = mediaElement.getVideoPlaybackQuality();
              var timeUpdateCount = 0;
              mediaElement.addEventListener("timeupdate", test.step_func(function (e)
              {
                  var videoElement = e.target;
                  var newQuality = videoElement.getVideoPlaybackQuality();

                  assert_not_equals(previousQuality, newQuality, "Verify new object");
                  assert_greater_than(newQuality.creationTime, previousQuality.creationTime, "creationTime");

                  assert_greater_than_equal(newQuality.totalVideoFrames, 0, "totalVideoFrames >= 0");
                  assert_greater_than_equal(newQuality.totalVideoFrames, previousQuality.totalVideoFrames, "totalVideoFrames");

                  assert_greater_than_equal(newQuality.droppedVideoFrames, 0, "droppedVideoFrames >= 0");
                  assert_greater_than_equal(newQuality.droppedVideoFrames, previousQuality.droppedVideoFrames, "droppedVideoFrames");

                  assert_greater_than_equal(newQuality.corruptedVideoFrames, 0, "corruptedVideoFrames >= 0");
                  assert_greater_than_equal(newQuality.corruptedVideoFrames, previousQuality.corruptedVideoFrames, "corruptedVideoFrames");
                  previousQuality = newQuality;
                  timeUpdateCount++;
              }));

              test.failOnEvent(mediaElement, 'error');

              test.expectEvent(sourceBuffer, "updatestart");
              test.expectEvent(sourceBuffer, "update");
              test.expectEvent(sourceBuffer, "updateend");
              sourceBuffer.appendBuffer(mediaData);
              assert_true(sourceBuffer.updating, "sourceBuffer.updating");

              test.waitForExpectedEvents(function()
              {
                  assert_false(sourceBuffer.updating, "sourceBuffer.updating");

                  test.expectEvent(sourceBuffer, "updatestart");
                  test.expectEvent(sourceBuffer, "update");
                  test.expectEvent(sourceBuffer, "updateend");

                  // Truncate the buffered media to about 1 second duration.
                  sourceBuffer.remove(1, +Infinity);
                  assert_true(sourceBuffer.updating, "sourceBuffer.updating");
              });

              test.waitForExpectedEvents(function()
              {
                  assert_false(sourceBuffer.updating, "sourceBuffer.updating");

                  mediaSource.endOfStream();
                  mediaElement.play();

                  test.expectEvent(mediaElement, "ended");
              });

              test.waitForExpectedEvents(function()
              {
                  assert_greater_than(timeUpdateCount, 2, "timeUpdateCount");
                  test.done();
              });
          }, "Test HTMLVideoElement.getVideoPlaybackQuality() with MediaSource API", {timeout: 5000});
        </script>
    </body>
</html>
